﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GIC.GERADOR
{
    public partial class TelaGeradora : Form
    {
        public TelaGeradora()
        {
            InitializeComponent();
        }

        private void lblHostname_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void somenteNumeros(KeyPressEventArgs e, TextBox Texto, Boolean isFloat)
        {
            if (e.KeyChar < 48 || e.KeyChar > 58)
            {
                if (e.KeyChar == 8)
                {
                    e.KeyChar = (char)8;
                    return;
                }
                if (isFloat)
                {
                    if (e.KeyChar == 44)
                    {
                        if (Texto.TextLength == 0)
                        {
                            e.KeyChar = (char)0;
                            return;
                        }
                        if (Texto.TextLength > 0)
                        {
                            if (Texto.Text.Contains(e.KeyChar.ToString()))
                            {
                                e.KeyChar = (char)0;
                                return;
                            }
                        }
                    }
                    else
                    {
                        e.KeyChar = (char)0;
                        return;
                    }
                }
                else
                {
                    e.KeyChar = (char)0;
                    return;
                }
            }
        }

        public void txtMeses_KeyPress(object sender, KeyPressEventArgs e) { somenteNumeros(e, txtMeses, false); }
        public void txtChaveAcesso_KeyPress(object sender, KeyPressEventArgs e) { e.KeyChar = (char)0; }

        private void btnImprimir_Click(object sender, EventArgs e)
        {

            if (this.txtMeses != null && this.txtMeses.Text != "")
            {

                DateTime dataInicial = DateTime.Now;

                DateTime dataFinal = DateTime.Now.AddMonths(Convert.ToInt32(this.txtMeses.Text)).AddDays(2);

                String chave = dataInicial.ToString("yyyyMMdd") + dataFinal.ToString("yyyyMMdd");

                int valor = new Random().Next(2, 7);

                String chaveCripto = valor.ToString();

                foreach (char carac in chave)
                {
                     String numero = (Convert.ToInt16(carac.ToString()) * valor).ToString();
                    if (numero.Length == 1)
                    {
                        numero = "0" + numero;
                    }
                    

                    
                    chaveCripto += numero;
                }
                this.txtChaveAcesso.Text = chaveCripto;

            }
            else
            {
                MessageBox.Show("Digite a quantidade de meses!", "Chave de Acesso", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
